package com.sds.frontcontroller;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sds.dao.MemberDAO;
import com.sds.util.Util_Servlet;
import com.sds.vo.MemberVO;

public class FrontController extends HttpServlet {		
	/**
	 * 
	 */
	private static final long serialVersionUID = -3466756170190703703L;

	// Front Controller Serlvet Class
	private HashMap<String, FrontController_Interface> controllerMap;
	private Properties properties;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		InputStream is = getClass().getClassLoader().getResourceAsStream("mvc.properties");

		controllerMap = new HashMap<String, FrontController_Interface>();
		properties = new Properties();

		try {
			properties.load(is);

			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		service(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		service(request, response);
	}

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);

		String uri = request.getRequestURI();
		String contextPath = request.getContextPath();
		String path = uri.substring(contextPath.length());
		
		FrontController_Interface frontController_Interface = controllerMap.get(path);

		if(frontController_Interface == null) {
			String className = properties.getProperty(path);
			//String className = properties.getProperty(path).trim();
			
			try {
				frontController_Interface = (FrontController_Interface)Class.forName(className).newInstance();

				controllerMap.put(path, frontController_Interface);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}			

		if("login".equals(request.getParameter("type"))) {
			String mem_id = request.getParameter("MEM_ID");
			String mem_pw = request.getParameter("MEM_PW");
						
			if(mem_id.length() == 0 || mem_pw.length() == 0)
				Util_Servlet.sendRedirect(response, "jsp_main/mainLogin.jsp");
			else {
				MemberDAO memberDAO = new MemberDAO();

				MemberVO memberVO = memberDAO.selectMember(mem_id);

				if(mem_id.equals(memberVO.getMem_id()) && mem_pw.equals(memberVO.getMem_pw())) {
					String mem_tel = memberVO.getMem_tel();
					
					session.setAttribute("MEMBER_STATUS", "LOGIN");
					session.setAttribute("MEMBER", memberVO);
					session.setAttribute("MEM_TEL_1", mem_tel.split("-")[0]);					
					session.setAttribute("MEM_TEL_2", mem_tel.split("-")[1]);
					session.setAttribute("MEM_TEL_3", mem_tel.split("-")[2]);
					
					if(memberVO.getMem_per().equals("1"))
						session.setAttribute("MEMBER_PERMISSION", "ADMIN");
					else
						session.setAttribute("MEMBER_PERMISSION", "NORMAL");
					
					Util_Servlet.forward(request, response, "jsp_main/mainLogin.jsp");
				} else {
					Util_Servlet.sendRedirect(response, "jsp_main/mainLogin.jsp");
				}
			}		
		} else if("logout".equals(request.getParameter("type"))) {		
			session.setAttribute("MEMBER_STATUS", "LOGOUT");
			session.removeAttribute("MEMBER");
			session.removeAttribute("MEM_TEL_1");
			session.removeAttribute("MEM_TEL_2");
			session.removeAttribute("MEM_TEL_3");
			session.removeAttribute("MEMBER_PERMISSION");
			
			session.invalidate();
			
			Util_Servlet.forward(request, response, "jsp_main/mainLogin.jsp");
		} else {
			frontController_Interface.execute(request, response);
		}
	}
}